/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dahg.project.controller.model.beans.impl;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import org.dahg.project.controller.model.Factura;
import org.dahg.project.controller.model.beans.AbstractBean;
import org.dahg.project.controller.model.exceptions.ValidationException;
import org.dahg.project.controller.model.services.local.PedidoService;

/**
 *
 * @author dahg
 */
@Stateless
public class PedidoServiceImpl extends AbstractBean<Factura> implements PedidoService {

    @Override
    protected Class<Factura> getClazz() {
        return Factura.class;
    }

    @Override
    public void remove(Factura obj) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Factura> getAllFacturas() {
        String sql = "select f from Factura f order by f.id desc";
        List<Factura> lista = getEntityManager().createQuery(sql, getClazz()).getResultList();
        return lista;
    }

    @Override
    public List<Factura> getAllFacturas(Date from, Date to) throws ValidationException {
        String sql = "select f from Factura f where f.cerrada=:is_cerrada and f.fecha between :from and :to  order by f.id desc";
        
        Query query = getEntityManager().createQuery(sql)
                .setParameter("from", from, TemporalType.DATE)
                .setParameter("to", to, TemporalType.DATE)
                .setParameter("is_cerrada", false);
        
        List<Factura> lista = query.getResultList();
        
        if(lista==null || lista.isEmpty()) throw new ValidationException("No se encontraron facturas");
        
        return lista;        
    }
    
}
